

use "$home\persondata_v3.dta", clear

merge 1:1 pnr year using "$project\manager", keep(1 3) nogen
replace manager=0 if manager==.
bysort pnr: egen maxmanager=max(manager)

ren pnr pnrm
merge m:1 pnr using "$project\managerfe_1step_over5", keep(1 3) nogen
ren pnrm pnr

keep if maxmanager==1

gen max_educ=int(hffsp/1000000)
gen educ=1 if max_educ<=25
replace educ=2 if max_educ==35
replace educ=3 if max_educ>=40&max_educ<=50
replace educ=4 if max_educ>=60&max_educ<.

ren me_bayes temp
bysort pnr: egen pe=max(temp)
drop temp

gen log_income=log(lonind)
gen log_hrwage=log(timelon)
gen log_totalincome=log(brutto)
gen expersq=exper^2

*managers own wages
*reghdfe log_income pe koen exper*, absorb(educ year#branche aldernov)

*manager selection over time
tsset pnr year
gen exit=(F.manager==0&manager==1&aldernov<62)
gen hardexit=(F.manager==0&manager==1&aldernov<62&F.brutto<0.9*brutto)
gen goodexit=(manager==1&F.lbnr!=lbnr&F.brutto>1.1*brutto&F.brutto!=.)

merge m:1 lbnr year using "$project\merger_lbnr", keep(1 3) keepus(target* nworker) nogen
merge m:1 cvrnr year using "$home\firmdata_v1.dta", keepus (valueadded fulltimeeq sales virkform) nogen keep(1 3)
*merge m:1 pnr using "$project/python/prediction", keep(1 3) nogen
merge m:1 lbnr year using "$project/ownership", keep(1 3) keepus(owner*) nogen
merge m:1 lbnr using "$project\family\familyfirm", keep(1 3) nogen
gen byte stockbased=(virkform==60)
gen byte solep=(virkform==10)

bysort pnr: egen minyear=min(year)
gen exper_manager=year-minyear
gen high_exper=(exper_manager>=5)
gen pe_high_exper=high_exper * pe

ren pnr pnr0
*keep top manager
merge m:1 cvrnr year using "$project\manager\manager_all_cvrnr2", keep(1 3) keepus(pnr) nogen
keep if pnr==pnr0

*Table 3

eststo clear

eststo: reghdfe exit pe koen aldernov exper*, absorb(educ#year year#branche) vce(cluster pnr)
eststo: reghdfe hardexit pe koen aldernov exper*, absorb(educ#year year#branche) vce(cluster pnr)
eststo: reghdfe goodexit pe koen aldernov exper*, absorb(educ#year year#branche) vce(cluster pnr)

eststo: reghdfe exit pe pe_high_exper high_exper koen aldernov exper*, absorb(educ#year year#branche) vce(cluster pnr)
eststo: reghdfe hardexit pe pe_high_exper high_exper koen aldernov exper*, absorb(educ#year year#branche) vce(cluster pnr)
eststo: reghdfe goodexit pe pe_high_exper high_exper koen aldernov exper*, absorb(educ#year year#branche) vce(cluster pnr)

cap drop sumtarget
bysort lbnr: egen temp=sum(target_lead)
bysort pnr: egen sumtarget=sum(temp)
drop temp 

eststo: reghdfe hardexit pe koen aldernov exper* if sumtarget==0, absorb(educ#year year#branche) vce(cluster pnr)

estout using "$project\results\table3.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) replace

/*


use "$project\merger_lbnr", clear

gen est_startyear=int(lbnr/1000000)
gen est_age=year-est_startyear
egen ind36num=group(ind36)
collapse (max) target_lead acquirer_lead (min) ind127 ind36num region (mean) est_age nest , by (cvrnr year)
gen singleest=(nest==1)
compress
save "$project/temp_ind_region", replace

use "$project\akm_1step_over5", clear
collapse pe fe, by (cvrnr year)
ren pe avgpe
ren fe cvrnrfe
save "$project\cvrnrfe_1step_over5", replace


*/

merge m:1 cvrnr year using "$home\firmdata_v1.dta", keepus(vapp) nogen keep(1 3)
merge m:1 cvrnr year using "$project\merger_cvrnr", keepus(logwage) nogen keep(1 3)
merge 1:1 cvrnr year using "$project\manager\cvrnryear_fe_new", keep(1 3) nogen
ren fe cvrnryearfe
merge 1:1 cvrnr year using "$project\temp_ind_region", keep(1 3) nogen
merge 1:1 cvrnr year using "$project\cvrnrfe_1step_over5", keep(1 3) nogen

bysort cvrnr: egen maxnworker=max(nworker)
gen byte nworker_group1=(nworker<20)
gen byte nworker_group2=(nworker>=20&nworker<50)
gen byte nworker_group3=(nworker>=50&nworker<=100)
gen byte nworker_group4=(nworker>100&nworker<=500)

gen byte estage_group0=(est_age<2)
gen byte estage_group1=(est_age<=5)
gen byte estage_group2=(est_age>5&est_age<=10)
gen byte estage_group3=(est_age>10&est_age<=20)

*Table 4

reghdfe target_lead logwage nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe target_lead cvrnryearfe nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe target_lead avgpe nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe target_lead vapp nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe target_lead pe nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe target_lead cvrnrfe nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)

reghdfe cvrnryearfe target_lead nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe pe target_lead nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe cvrnrfe target_lead nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe cvrnryearfe acquirer_lead nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe pe acquirer nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe cvrnrfe acquirer nworker_group* estage_group* singleest [aw=nworker] if pe!=., absorb(ind127#year region#year)
reghdfe pe target_lead nworker_group* estage_group* singleest [aw=nworker] if pe!=. & high_exper==1, absorb(ind127#year region#year)
reghdfe pe target_lead nworker_group* estage_group* singleest [aw=nworker] if pe!=. & high_exper==0, absorb(ind127#year region#year)





*which firms hire high FE managers (Table A6)

gen lognworker=log(nworker)

merge m:1 ind127 using "$project/hhi", keep(1 3) nogen

xtile decile_size=nworker, n(10)

bysort ind127 region year decile_size: egen nmanager=nvals(pnr)
gen lognmanager=log(nmanager)

eststo clear

eststo: reghdfe pe est_age, absorb(ind127#year region#year)
eststo: reghdfe pe lognworker, absorb(ind127#year region#year)
eststo: reghdfe pe family lognworker, absorb(ind127#year region#year)
eststo: reghdfe pe stockbased lognmanager, absorb(ind127#year region#year)
eststo: reghdfe pe hhi, a(region#year)

estout using "$project\results\whichfirm.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) replace









